package com.itheima.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.google.gson.internal.$Gson$Preconditions;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/map")
public class AddressController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private AddressService addressService;

    //文件上传

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile")MultipartFile imgFile){
        try{
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(lastIndexOf - 1);
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //图片上传成功
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
            result.setData(fileName);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            //图片上传失败
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }



    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = addressService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }


    @RequestMapping("/add")
    public Result add(@RequestBody Address address) {
        //因为前段传来的是json格式的数据，需要加入requestbody来封装
        try {
            addressService.add(address);
            jedisPool.getResource().srem("health-img", address.getImg());
        } catch (Exception e) {
            //服务调用失败
            return new Result(false, "地图新增失败");
        }
        return new Result(true, "地图新增成功");
    }


    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            addressService.deleteById(id);
        } catch (Exception e) {
            return new Result(false, "地图删除失败");
        }
        return new Result(true, "地图删除成功");
    }


    @RequestMapping("/edit")
    public Result edit(@RequestBody Address address) {
        try {
            addressService.edit(address);
        } catch (Exception e) {
            return new Result(false, "编辑失败");
        }
        return new Result(true, "编辑成功");
    }


    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Address address = null;
        try {
            address = addressService.findById(id);
        } catch (Exception e) {
            return new Result(false, "查询失败");
        }
        return new Result(true, "查询成功", address);
    }


    @RequestMapping("/findAllmap")
    public List<Address>findAllmap(){
        List<Address> list = addressService.findAllmap();
        for (Address address : list) {
            System.out.println(address.getLl());
        }
        return list;
    }
    @RequestMapping("/findAllAddress")
    public Result findAllAddress(){

        try {
            String address = addressService.findAllAddress();
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,address);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }





    }



}

